class Solution(object):
	#归并
    def mergeList(self, l1, l2):
        dummyHead = ListNode()
        cur = dummyHead
        while l1 and l2:
            if l1.val < l2.val:
                cur.next = l1
                l1 = l1.next
            else:
                cur.next = l2
                l2 = l2.next
            cur = cur.next
        if l1:
            cur.next = l1
        if l2:
            cur.next = l2
        return dummyHead.next

    def sortList(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        #find mid
        fast = head
        slow = head
        if head is None or head.next is None:
            return head

        while fast and fast.next:
            mid = slow
            slow = slow.next
            fast = fast.next.next
        #在mid处断开链接，一分为二
        mid.next = None
        left = self.sortList(head)
        right = self.sortList(slow)
        return self.mergeList(left, right)
